home *** CD-ROM | disk | FTP | other *** search
- ##
- ### "Mdepend.awk" - Scan for #includes and generate make dependency lines
- ###
- ### v1.1(01) 20 Feb 1990 John Elliott IV
- ##
-
- BEGIN {
- version="1.1(01)";
- print "READING:" > "/dev/tty";
- }
-
- lastfile != FILENAME {
- printf(" %s:\n", FILENAME) > "/dev/tty";
- lastfile = FILENAME;
-
- # Keep track of all files scanned
- files[nfiles++] = lastfile;
- }
-
- /^#[ ]*include[ ]*"/ {
- ###################################################
- # Find the opening quote or less-than
- ###################################################
- for (i = 1; i <= length; ++i) {
- opening = substr($0, i, 1);
- if (opening == "\"" || opening == "<")
- break;
- }
- ###################################################
- # Find the closing quote or greater-than
- ###################################################
- closing = opening;
- if (closing == "<") closing = ">";
- len = index(substr($0, ++i, length), closing);
- if (len == 0)
- len = length + 1;
- else
- len = len - 1;
-
- ###################################################
- # Extract the file name
- ###################################################
- file = substr($0, i, len);
- # printf("\t%-24s substr(\"%s\", %d, %d)\n",
- # opening file closing, $0, i, len) > "/dev/tty";
-
- ###################################################
- # Remember if this is a system include file
- ###################################################
- if (closing == ">")
- file = "<" file ">";
-
- ###################################################
- # Show what we found
- ###################################################
- #printf("\t%s\n", file) > "/dev/tty";
-
-
- #######################################################################
- # Remember what we've found:
- # sources[] - A list of files that #include and who they include
- # (indexed by filename and numerically indexed
- # so we can loop thru all names)
- #######################################################################
- if (sources[FILENAME] == "")
- sources[nsources++] = FILENAME;
- sources[FILENAME] = sources[FILENAME] " " file;
- }
-
-
- END {
- printf("\nPROCESSING:\n") > "/dev/tty";
-
- #######################################################################
- # Now, for each file, we look at each file it includes.
- # If it is listed in the sources, look to see if THAT file
- # includes other files, and if it does, check THAT file, & etc
- # until we have a list of all files this target depends on.
- #######################################################################
- for (i = 0; i < nsources; ++i) {
- sname = sources[i];
- printf("\t%s", sname) > "/dev/tty";
- ###############################################
- # Look at all files "sname" includes
- ###############################################
- oj = nj = split(sources[sname], list);
- for (j = 1; j <= nj; ++j)
- list[list[j]] = j; # Remember which are here already
- for (j = 1; j <= nj; ++j) {
- iname = list[j];
- #######################################################
- # If this file itself includes files, add them to the
- # to-be-scanned list if they're not already there
- # (Yes, I know; this could go on forever)
- #######################################################
- if (sources[iname] != "") {
- nk = split(sources[iname], new);
- ##################################################
- # For each file that this one includes, see if
- # we have already added it to our list. If not,
- # add it to the "depends-on" list.
- ##################################################
- for (k = 1; k <= nk; ++k) {
- if (list[new[k]] == "") {
- #if (oj == nj)
- # printf(" - adding:") > "/dev/tty";
- #printf(" %s", new[k]) > "/dev/tty";
- list[++nj] = new[k];
- list[new[k]] = nj;
- sources[sname] = sources[sname] " " new[k];
- }
- }
- }
- }
- printf("\n") > "/dev/tty";
- }
-
- printf("Updating make dependency lines\n") > "/dev/tty";
- ##################################################################
- # Tell `ed' to change everything between our two special lines:
- ##################################################################
- printf("/^# DO NOT DELETE THIS LINE/,/^# ALSO DO NOT DELETE THIS/ c\n");
- printf("# DO NOT DELETE THIS LINE - \"make depend\" requires it.\n");
- printf("# Generated by Mdepend.awk version %s\n", version);
- printf("# The dependencies were generated by examining the following files:");
- ll = 80;
- for (i = 0; i < nfiles; ++i) {
- file = files[i];
- wl = length(file);
- if (ll + wl + 1 > 78) {
- printf("\n# ");
- ll = 5;
- }
- printf(" %s", file);
- ll += wl + 1;
- }
- printf("\n");
-
- for (i = 0; i < nsources; ++i) {
- file = sources[i];
- # Find the extension (if any)
- for (extndx = length(file); extndx > 0; --extndx) {
- if (substr(file, extndx, 1) == ".")
- break;
- }
- if (extndx == 0)
- ext = "";
- else
- ext = substr(file, extndx, length(file));
- # In case they forget and give us a non-code-generating file...
- if (ext == ".h")
- continue;
-
- # Now chop any path prefix
- for (ndent = length(file); ndent > 0; --ndent) {
- if (substr(file, ndent, 1) == "/")
- break;
- }
- ++ndent; # Skip the slash, or start at '1'
-
- # Finally, output the dependency lines
- printf("%s.o:", substr(file, ndent, extndx - ndent));
- deps = sources[file];
- nj = split(deps, list);
- ll = length(file) + 1;
- for (j = 1; j <= nj; ++j) {
- wl = length(list[j]);
- if (ll + wl + 1 > 78) {
- printf("\\\n\t%s", list[j]);
- ll = 8 + wl;
- }
- else {
- ll += wl + 1;
- printf(" %s", list[j]);
- }
- }
- printf("\n");
- }
- printf("# ALSO DO NOT DELETE THIS LINE - \"make depend\" requires it.\n");
- # Finally, tell ed to quit changing, write the new file out, and quit
- printf(".\nw\nq\n");
- }
-